﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Power.DBUtility;//Please add references
namespace Power.DAL
{
	/// <summary>
	/// 数据访问类:Meter_infoDAO
	/// </summary>
	public partial class Meter_infoDAO
	{
		public Meter_infoDAO()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string meter_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from meter_info");
			strSql.Append(" where meter_id=@meter_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@meter_id", SqlDbType.VarChar,50)			};
			parameters[0].Value = meter_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Power.Model.Meter_info model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into meter_info(");
			strSql.Append("meter_id,meter_name,station_id,chest_id,entery_time,overdue_date,logo,kind,is_use,keyin_user,keyin_time,rated_current,rated_current_60,node,com_port)");
			strSql.Append(" values (");
			strSql.Append("@meter_id,@meter_name,@station_id,@chest_id,@entery_time,@overdue_date,@logo,@kind,@is_use,@keyin_user,@keyin_time,@rated_current,@rated_current_60,@node,@com_port)");
			SqlParameter[] parameters = {
					new SqlParameter("@meter_id", SqlDbType.VarChar,50),
					new SqlParameter("@meter_name", SqlDbType.VarChar,100),
					new SqlParameter("@station_id", SqlDbType.VarChar,20),
					new SqlParameter("@chest_id", SqlDbType.VarChar,50),
					new SqlParameter("@entery_time", SqlDbType.DateTime),
					new SqlParameter("@overdue_date", SqlDbType.DateTime),
					new SqlParameter("@logo", SqlDbType.VarChar,50),
					new SqlParameter("@kind", SqlDbType.VarChar,50),
					new SqlParameter("@is_use", SqlDbType.Int,4),
					new SqlParameter("@keyin_user", SqlDbType.VarChar,20),
					new SqlParameter("@keyin_time", SqlDbType.DateTime),
					new SqlParameter("@rated_current", SqlDbType.Float,8),
					new SqlParameter("@rated_current_60", SqlDbType.Float,8),
					new SqlParameter("@node", SqlDbType.Int,4),
					new SqlParameter("@com_port", SqlDbType.VarChar,20)};
			parameters[0].Value = model.meter_id;
			parameters[1].Value = model.meter_name;
			parameters[2].Value = model.station_id;
			parameters[3].Value = model.chest_id;
			parameters[4].Value = model.entery_time;
			parameters[5].Value = model.overdue_date;
			parameters[6].Value = model.logo;
			parameters[7].Value = model.kind;
			parameters[8].Value = model.is_use;
			parameters[9].Value = model.keyin_user;
			parameters[10].Value = model.keyin_time;
			parameters[11].Value = model.rated_current;
			parameters[12].Value = model.rated_current_60;
			parameters[13].Value = model.node;
			parameters[14].Value = model.com_port;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Power.Model.Meter_info model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update meter_info set ");
			strSql.Append("meter_name=@meter_name,");
			strSql.Append("station_id=@station_id,");
			strSql.Append("chest_id=@chest_id,");
			strSql.Append("entery_time=@entery_time,");
			strSql.Append("overdue_date=@overdue_date,");
			strSql.Append("logo=@logo,");
			strSql.Append("kind=@kind,");
			strSql.Append("is_use=@is_use,");
			strSql.Append("keyin_user=@keyin_user,");
			strSql.Append("keyin_time=@keyin_time,");
			strSql.Append("rated_current=@rated_current,");
			strSql.Append("rated_current_60=@rated_current_60,");
			strSql.Append("node=@node,");
			strSql.Append("com_port=@com_port");
			strSql.Append(" where meter_id=@meter_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@meter_name", SqlDbType.VarChar,100),
					new SqlParameter("@station_id", SqlDbType.VarChar,20),
					new SqlParameter("@chest_id", SqlDbType.VarChar,50),
					new SqlParameter("@entery_time", SqlDbType.DateTime),
					new SqlParameter("@overdue_date", SqlDbType.DateTime),
					new SqlParameter("@logo", SqlDbType.VarChar,50),
					new SqlParameter("@kind", SqlDbType.VarChar,50),
					new SqlParameter("@is_use", SqlDbType.Int,4),
					new SqlParameter("@keyin_user", SqlDbType.VarChar,20),
					new SqlParameter("@keyin_time", SqlDbType.DateTime),
					new SqlParameter("@rated_current", SqlDbType.Float,8),
					new SqlParameter("@rated_current_60", SqlDbType.Float,8),
					new SqlParameter("@node", SqlDbType.Int,4),
					new SqlParameter("@com_port", SqlDbType.VarChar,20),
					new SqlParameter("@meter_id", SqlDbType.VarChar,50)};
			parameters[0].Value = model.meter_name;
			parameters[1].Value = model.station_id;
			parameters[2].Value = model.chest_id;
			parameters[3].Value = model.entery_time;
			parameters[4].Value = model.overdue_date;
			parameters[5].Value = model.logo;
			parameters[6].Value = model.kind;
			parameters[7].Value = model.is_use;
			parameters[8].Value = model.keyin_user;
			parameters[9].Value = model.keyin_time;
			parameters[10].Value = model.rated_current;
			parameters[11].Value = model.rated_current_60;
			parameters[12].Value = model.node;
			parameters[13].Value = model.com_port;
			parameters[14].Value = model.meter_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string meter_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from meter_info ");
			strSql.Append(" where meter_id=@meter_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@meter_id", SqlDbType.VarChar,50)			};
			parameters[0].Value = meter_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string meter_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from meter_info ");
			strSql.Append(" where meter_id in ("+meter_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Power.Model.Meter_info GetModel(string meter_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 meter_id,meter_name,station_id,chest_id,entery_time,overdue_date,logo,kind,is_use,keyin_user,keyin_time,rated_current,rated_current_60,node,com_port from meter_info ");
			strSql.Append(" where meter_id=@meter_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@meter_id", SqlDbType.VarChar,50)			};
			parameters[0].Value = meter_id;

			Power.Model.Meter_info model=new Power.Model.Meter_info();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Power.Model.Meter_info DataRowToModel(DataRow row)
		{
			Power.Model.Meter_info model=new Power.Model.Meter_info();
			if (row != null)
			{
				if(row["meter_id"]!=null)
				{
					model.meter_id=row["meter_id"].ToString();
				}
				if(row["meter_name"]!=null)
				{
					model.meter_name=row["meter_name"].ToString();
				}
				if(row["station_id"]!=null)
				{
					model.station_id=row["station_id"].ToString();
				}
				if(row["chest_id"]!=null)
				{
					model.chest_id=row["chest_id"].ToString();
				}
				if(row["entery_time"]!=null && row["entery_time"].ToString()!="")
				{
					model.entery_time=DateTime.Parse(row["entery_time"].ToString());
				}
				if(row["overdue_date"]!=null && row["overdue_date"].ToString()!="")
				{
					model.overdue_date=DateTime.Parse(row["overdue_date"].ToString());
				}
				if(row["logo"]!=null)
				{
					model.logo=row["logo"].ToString();
				}
				if(row["kind"]!=null)
				{
					model.kind=row["kind"].ToString();
				}
				if(row["is_use"]!=null && row["is_use"].ToString()!="")
				{
					model.is_use=int.Parse(row["is_use"].ToString());
				}
				if(row["keyin_user"]!=null)
				{
					model.keyin_user=row["keyin_user"].ToString();
				}
				if(row["keyin_time"]!=null && row["keyin_time"].ToString()!="")
				{
					model.keyin_time=DateTime.Parse(row["keyin_time"].ToString());
				}
				if(row["rated_current"]!=null && row["rated_current"].ToString()!="")
				{
					model.rated_current=decimal.Parse(row["rated_current"].ToString());
				}
				if(row["rated_current_60"]!=null && row["rated_current_60"].ToString()!="")
				{
					model.rated_current_60=decimal.Parse(row["rated_current_60"].ToString());
				}
				if(row["node"]!=null && row["node"].ToString()!="")
				{
					model.node=int.Parse(row["node"].ToString());
				}
				if(row["com_port"]!=null)
				{
					model.com_port=row["com_port"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select meter_id,meter_name,station_id,chest_id,entery_time,overdue_date,logo,kind,is_use,keyin_user,keyin_time,rated_current,rated_current_60,node,com_port ");
			strSql.Append(" FROM meter_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" meter_id,meter_name,station_id,chest_id,entery_time,overdue_date,logo,kind,is_use,keyin_user,keyin_time,rated_current,rated_current_60,node,com_port ");
			strSql.Append(" FROM meter_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM meter_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.meter_id desc");
			}
			strSql.Append(")AS Row, T.*  from meter_info T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "meter_info";
			parameters[1].Value = "meter_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

